package com.artifact.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.artifact.dao.YungouPublishDao;
import com.artifact.model.YungouPublish;

@Repository("yungouPublishDao")
public class YungouPublishDaoImpl implements YungouPublishDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public boolean addYungouPublish(int goodsID, int codePeriod, int codeID, int codeRNO, int awardPosition,
                                    String userName, int buyCount, String buyStartPosition) {
        String sql = "INSERT INTO yungou_publish (`goodsID`, `codePeriod`, `codeID`, `codeRNO`, `awardPosition`, `userName`, `buyCount`, `buyStartPosition`) VALUES(?,?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql, goodsID, codePeriod, codeID, codeRNO, awardPosition, userName, buyCount,
                buyStartPosition) > 0;
    }

    @Override
    public YungouPublish getYungouPublish(int goodsID, int codePeriod) {
        String sql = "SELECT * FROM yungou_publish where goodsId=? and codePeriod = ?";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<YungouPublish>(YungouPublish.class), goodsID,
                    codePeriod);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<YungouPublish> getYungouPublishs(int goodsID, int selectNum) {
        try {
            String sql = "SELECT * FROM yungou_publish where goodsId=? order by codePeriod desc limit 0,?";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<YungouPublish>(YungouPublish.class), goodsID, selectNum);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public void deleteYungouPublish(int goodsID, int codePeriod) {
        String sql = "DELETE FROM yungou_publish where goodsId=? and codePeriod = ?";
        jdbcTemplate.update(sql, goodsID, codePeriod);
    }

}
